{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/usr/local/lib/python3.6/site-packages/tensorflow/python/framework/dtypes.py:516: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.\n",
      "  _np_qint8 = np.dtype([(\"qint8\", np.int8, 1)])\n",
      "/usr/local/lib/python3.6/site-packages/tensorflow/python/framework/dtypes.py:517: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.\n",
      "  _np_quint8 = np.dtype([(\"quint8\", np.uint8, 1)])\n",
      "/usr/local/lib/python3.6/site-packages/tensorflow/python/framework/dtypes.py:518: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.\n",
      "  _np_qint16 = np.dtype([(\"qint16\", np.int16, 1)])\n",
      "/usr/local/lib/python3.6/site-packages/tensorflow/python/framework/dtypes.py:519: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.\n",
      "  _np_quint16 = np.dtype([(\"quint16\", np.uint16, 1)])\n",
      "/usr/local/lib/python3.6/site-packages/tensorflow/python/framework/dtypes.py:520: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.\n",
      "  _np_qint32 = np.dtype([(\"qint32\", np.int32, 1)])\n",
      "/usr/local/lib/python3.6/site-packages/tensorflow/python/framework/dtypes.py:525: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.\n",
      "  np_resource = np.dtype([(\"resource\", np.ubyte, 1)])\n",
      "/usr/local/lib/python3.6/site-packages/tensorboard/compat/tensorflow_stub/dtypes.py:541: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.\n",
      "  _np_qint8 = np.dtype([(\"qint8\", np.int8, 1)])\n",
      "/usr/local/lib/python3.6/site-packages/tensorboard/compat/tensorflow_stub/dtypes.py:542: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.\n",
      "  _np_quint8 = np.dtype([(\"quint8\", np.uint8, 1)])\n",
      "/usr/local/lib/python3.6/site-packages/tensorboard/compat/tensorflow_stub/dtypes.py:543: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.\n",
      "  _np_qint16 = np.dtype([(\"qint16\", np.int16, 1)])\n",
      "/usr/local/lib/python3.6/site-packages/tensorboard/compat/tensorflow_stub/dtypes.py:544: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.\n",
      "  _np_quint16 = np.dtype([(\"quint16\", np.uint16, 1)])\n",
      "/usr/local/lib/python3.6/site-packages/tensorboard/compat/tensorflow_stub/dtypes.py:545: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.\n",
      "  _np_qint32 = np.dtype([(\"qint32\", np.int32, 1)])\n",
      "/usr/local/lib/python3.6/site-packages/tensorboard/compat/tensorflow_stub/dtypes.py:550: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.\n",
      "  np_resource = np.dtype([(\"resource\", np.ubyte, 1)])\n"
     ]
    }
   ],
   "source": [
    "import time\n",
    "import torch\n",
    "import numpy as np\n",
    "from DcBert import DcBert"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 0 0 0]\n",
      "[1.7791 1.7261 1.7791 1.7261]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import torch\n",
    "x = torch.tensor([[ 1.7791, -1.4819],\n",
    "        [ 1.7261, -1.3455],\n",
    "        [ 1.7791, -1.4819],\n",
    "        [ 1.7261, -1.3455]])\n",
    "x=x.numpy()\n",
    "print(np.argmax(x,axis=1))\n",
    "print(np.max(x,axis=1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\u001b[0;31mSignature:\u001b[0m\n",
       "\u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmax\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\u001b[0m\n",
       "\u001b[0;34m\u001b[0m    \u001b[0ma\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n",
       "\u001b[0;34m\u001b[0m    \u001b[0maxis\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n",
       "\u001b[0;34m\u001b[0m    \u001b[0mout\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n",
       "\u001b[0;34m\u001b[0m    \u001b[0mkeepdims\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m<\u001b[0m\u001b[0mno\u001b[0m \u001b[0mvalue\u001b[0m\u001b[0;34m>\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n",
       "\u001b[0;34m\u001b[0m    \u001b[0minitial\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m<\u001b[0m\u001b[0mno\u001b[0m \u001b[0mvalue\u001b[0m\u001b[0;34m>\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n",
       "\u001b[0;34m\u001b[0m    \u001b[0mwhere\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m<\u001b[0m\u001b[0mno\u001b[0m \u001b[0mvalue\u001b[0m\u001b[0;34m>\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n",
       "\u001b[0;34m\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
       "\u001b[0;31mDocstring:\u001b[0m\n",
       "Return the maximum of an array or maximum along an axis.\n",
       "\n",
       "Parameters\n",
       "----------\n",
       "a : array_like\n",
       "    Input data.\n",
       "axis : None or int or tuple of ints, optional\n",
       "    Axis or axes along which to operate.  By default, flattened input is\n",
       "    used.\n",
       "\n",
       "    .. versionadded:: 1.7.0\n",
       "\n",
       "    If this is a tuple of ints, the maximum is selected over multiple axes,\n",
       "    instead of a single axis or all the axes as before.\n",
       "out : ndarray, optional\n",
       "    Alternative output array in which to place the result.  Must\n",
       "    be of the same shape and buffer length as the expected output.\n",
       "    See `doc.ufuncs` (Section \"Output arguments\") for more details.\n",
       "\n",
       "keepdims : bool, optional\n",
       "    If this is set to True, the axes which are reduced are left\n",
       "    in the result as dimensions with size one. With this option,\n",
       "    the result will broadcast correctly against the input array.\n",
       "\n",
       "    If the default value is passed, then `keepdims` will not be\n",
       "    passed through to the `amax` method of sub-classes of\n",
       "    `ndarray`, however any non-default value will be.  If the\n",
       "    sub-class' method does not implement `keepdims` any\n",
       "    exceptions will be raised.\n",
       "\n",
       "initial : scalar, optional\n",
       "    The minimum value of an output element. Must be present to allow\n",
       "    computation on empty slice. See `~numpy.ufunc.reduce` for details.\n",
       "\n",
       "    .. versionadded:: 1.15.0\n",
       "\n",
       "where : array_like of bool, optional\n",
       "    Elements to compare for the maximum. See `~numpy.ufunc.reduce`\n",
       "    for details.\n",
       "\n",
       "    .. versionadded:: 1.17.0\n",
       "\n",
       "Returns\n",
       "-------\n",
       "amax : ndarray or scalar\n",
       "    Maximum of `a`. If `axis` is None, the result is a scalar value.\n",
       "    If `axis` is given, the result is an array of dimension\n",
       "    ``a.ndim - 1``.\n",
       "\n",
       "See Also\n",
       "--------\n",
       "amin :\n",
       "    The minimum value of an array along a given axis, propagating any NaNs.\n",
       "nanmax :\n",
       "    The maximum value of an array along a given axis, ignoring any NaNs.\n",
       "maximum :\n",
       "    Element-wise maximum of two arrays, propagating any NaNs.\n",
       "fmax :\n",
       "    Element-wise maximum of two arrays, ignoring any NaNs.\n",
       "argmax :\n",
       "    Return the indices of the maximum values.\n",
       "\n",
       "nanmin, minimum, fmin\n",
       "\n",
       "Notes\n",
       "-----\n",
       "NaN values are propagated, that is if at least one item is NaN, the\n",
       "corresponding max value will be NaN as well. To ignore NaN values\n",
       "(MATLAB behavior), please use nanmax.\n",
       "\n",
       "Don't use `amax` for element-wise comparison of 2 arrays; when\n",
       "``a.shape[0]`` is 2, ``maximum(a[0], a[1])`` is faster than\n",
       "``amax(a, axis=0)``.\n",
       "\n",
       "Examples\n",
       "--------\n",
       ">>> a = np.arange(4).reshape((2,2))\n",
       ">>> a\n",
       "array([[0, 1],\n",
       "       [2, 3]])\n",
       ">>> np.amax(a)           # Maximum of the flattened array\n",
       "3\n",
       ">>> np.amax(a, axis=0)   # Maxima along the first axis\n",
       "array([2, 3])\n",
       ">>> np.amax(a, axis=1)   # Maxima along the second axis\n",
       "array([1, 3])\n",
       ">>> np.amax(a, where=[False, True], initial=-1, axis=0)\n",
       "array([-1,  3])\n",
       ">>> b = np.arange(5, dtype=float)\n",
       ">>> b[2] = np.NaN\n",
       ">>> np.amax(b)\n",
       "nan\n",
       ">>> np.amax(b, where=~np.isnan(b), initial=-1)\n",
       "4.0\n",
       ">>> np.nanmax(b)\n",
       "4.0\n",
       "\n",
       "You can use an initial value to compute the maximum of an empty slice, or\n",
       "to initialize it to a different value:\n",
       "\n",
       ">>> np.max([[-50], [10]], axis=-1, initial=0)\n",
       "array([ 0, 10])\n",
       "\n",
       "Notice that the initial value is used as one of the elements for which the\n",
       "maximum is determined, unlike for the default argument Python's max\n",
       "function, which is only used for empty iterables.\n",
       "\n",
       ">>> np.max([5], initial=6)\n",
       "6\n",
       ">>> max([5], default=6)\n",
       "5\n",
       "\u001b[0;31mFile:\u001b[0m      /usr/local/lib/python3.6/site-packages/numpy/core/fromnumeric.py\n",
       "\u001b[0;31mType:\u001b[0m      function\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "np.max?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "model = DcBert()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<All keys matched successfully>"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.load_state_dict(torch.load('./output/bert.ckpt'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "DEVICE = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')\n",
    "model.to(DEVICE)\n",
    "model.eval()\n",
    "model.build_index([\"我这里有房子\",\"测试一下\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "预测耗费时间：0.03353762626647949s\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "tensor([[ 5.1012, -3.9142],\n",
       "        [ 6.1313, -4.9896],\n",
       "        [ 5.1012, -3.9142],\n",
       "        [ 6.1313, -4.9896]], device='cuda:0', grad_fn=<AddmmBackward>)"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.search_all(\"有房子吗，想去看下\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n"
     ]
    }
   ],
   "source": [
    "import torch\n",
    "x = torch.randn([2, 3, 4])\n",
    "x = x[:, 0, :].unsqueeze(1)\n",
    "y = torch.randn([2, 5, 4])\n",
    "y = y[:, 0, :].unsqueeze(1)\n",
    "z = torch.cat([x, y], 1)\n",
    "print(z.shape[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "torch.Size([2, 8])\n"
     ]
    }
   ],
   "source": [
    "z = torch.reshape(z,(z.shape[0],-1))\n",
    "print(z.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "import random\n",
    "x =[[1,2,1],[1,2,2],[3,2,3],[4,3,2],[5,5,5]]\n",
    "random.shuffle(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[4, 3, 2], [1, 2, 1], [1, 2, 2], [3, 2, 3], [5, 5, 5]]\n"
     ]
    }
   ],
   "source": [
    "print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
